/**
 * User: zsquirrel
 * Date: 2020/5/1
 * Time: 10:12 上午
 */
package com.cskaoyan.mall.service;

import com.cskaoyan.mall.dao.*;
import com.cskaoyan.mall.model.Order;
import com.cskaoyan.mall.model.Spec;
import com.cskaoyan.mall.model.User;
import com.cskaoyan.mall.model.bo.ChangeOrderBO;
import com.cskaoyan.mall.model.bo.OrderRequestParamBO;
import com.cskaoyan.mall.model.bo.OrderedGoodsInfoBO;
import com.cskaoyan.mall.model.bo.PlaceOrderBO;
import com.cskaoyan.mall.model.enmu.OrderState;
import com.cskaoyan.mall.model.vo.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {

    private OrderDao orderDao = new OrderDaoImpl();

    private GoodsDao goodsDao = new GoodsDaoImpl();

    private UserDao userDao = new UserDaoImpl();

    @Override
    public OrderResponseVO ordersByPage(OrderRequestParamBO param) {
        //多条件查询
        //查询两次 第一次根据条件查询出total
        Integer total = orderDao.getTotal(param);
        //根据条件查询出具体的订单数据
        List<Order> orderList = orderDao.ordersByPage(param);
        for (Order order : orderList) {
            UserVO user = new UserVO(order.getNickname(),order.getName(),order.getAddress(),order.getPhone());
            order.setUser(user);
            //取出stateId，然后根据不同，设置不同的state的值
        }
        OrderResponseVO orderResponseVO = new OrderResponseVO();
        orderResponseVO.setTotal(total);
        orderResponseVO.setOrders(orderList);
        return orderResponseVO;
    }

    @Override
    public OrderInfoVO getOrderInfo(String id) {
        Order order = orderDao.getOrderInfo(id);
        List<Spec> specs = goodsDao.getSiblingSpecs(order.getGoodsDetailId());
        OrderState[] values = OrderState.values();
        List<StateVO> states = new ArrayList<>();
        for (OrderState value : values) {
            StateVO stateVO = new StateVO(value.getId(), value.getName());
            states.add(stateVO);
        }
        OrderInfoVO orderInfoVO = new OrderInfoVO();
        orderInfoVO.setId(order.getId());
        orderInfoVO.setAmount(order.getAmount());
        orderInfoVO.setNum(order.getGoodsNum());
        orderInfoVO.setGoodsDetailId(order.getGoodsDetailId());
        orderInfoVO.setState(order.getStateId());
        orderInfoVO.setGoods(order.getGoods());
        orderInfoVO.setSpec(specs);
        orderInfoVO.setStates(states);
        Map<String, Object> curState = new HashMap<>();
        curState.put("id", order.getStateId());
        Map<String, Object> curSpec = new HashMap<>();
        curSpec.put("id", order.getGoodsDetailId());
        orderInfoVO.setCurSpec(curSpec);
        orderInfoVO.setCurState(curState);
        return orderInfoVO;
    }

    @Override
    public void changeOrder(ChangeOrderBO changeOrderBO) {
        orderDao.changeOrder(changeOrderBO);
    }

    @Override
    public void deleteOrder(String id) {
        orderDao.deleteOrder(id);
    }

    @Override
    public void placeOrder(PlaceOrderBO placeOrderBO) {
        //根据token查出user信息
        //根据goodsDetailId查询出相关的商品和规格信息
        //保存入库
        List<User> users = userDao.searchUser(placeOrderBO.getToken());
        User user = users.get(0);
        OrderedGoodsInfoBO orderedGoodsInfoBO = goodsDao.getOrderedGoodsInfo(placeOrderBO.getGoodsDetailId());
        Order order = new Order();
        order.setUserId(user.getId());
        order.setGoodsDetailId(orderedGoodsInfoBO.getGoodsDetailId());
        order.setGoods(orderedGoodsInfoBO.getGoods());
        order.setSpec(orderedGoodsInfoBO.getSpec());
        order.setGoodsNum(placeOrderBO.getNum());
        order.setAmount(placeOrderBO.getAmount());
        order.setStateId(placeOrderBO.getState());
        order.setNickname(user.getNickname());
        order.setName(user.getRecipient());
        order.setAddress(user.getAddress());
        order.setPhone(user.getPhone());
        order.setImg(orderedGoodsInfoBO.getImg());
        order.setUnitPrice(orderedGoodsInfoBO.getUnitPrice());
        order.setGoodsId(orderedGoodsInfoBO.getGoodsId());
        orderDao.placeOrder(order);
        //商品库存减少
        goodsDao.updateStockNum(order.getGoodsNum(), order.getGoodsDetailId());
    }

    @Override
    public List<StateOrderVO> getOrderByState(String state, String token) {
        List<User> users = userDao.searchUser(token);
        Integer userId = users.get(0).getId();
        List<StateOrderVO> orderVOList = orderDao.getOrderByState(state, userId);
        return orderVOList;
    }
}
